package cn.cloudreal.cbms.accrual.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.accrual.service.AccrualService;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.vo.ContractAccrual;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.ItemAccrual;
import com.opensymphony.xwork2.ActionSupport;
import cn.cloudreal.cbms.vo.UserInfo;

public class AccrualAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	/** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(AccrualAction.class);
	
	/** 服务 */
    AccrualService accrualService;
    
	/** 当前页 */
    int currentPage;

	/** 分页对象 */
    PageBean page;
    
	/** 计提明细列表（统购总体） */
    List<ItemAccrual> itemAccruals;
    
    /** 计提明细列表（备选） */
    List<ItemAccrual> itemBackAccruals;
    
    /** 计提明细列表（合同） */
    List<ContractAccrual> itemContractAccruals;
    
    /** 计提截止时间 */
	private String totalAccrualEndTime = null;
    
    /** 媒体类型 */
	private String mediaType = null;
	
	/** 归属地市 */
	private String accrualCity;  

	/**查询类别*/
    private Integer accrualType = null;

   	/** 关键字 */
    String keyword;
    String keywordbx;
    String keywordht;

    /** 媒体类型下拉 */
    private List<Dictionary> mediaTypeSelect = null;
    
    /** 归属地市下拉 */
    private List<Dictionary> dataListCITY = null;
   
    /** 关键字下拉菜单 */
	private int keywordList; 


	//获取业务计提明细（统购及总体）
	@SuppressWarnings("unchecked")
	public String totalAccrualList() 
	{
		
		// 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter AccrualAction totalAccrualList() method, return String");
        }

        try
        {
        getDictionary();            //获取字典表中归属地
    	if (StringUtils.isEmpty(totalAccrualEndTime)) 
    	{
       	  totalAccrualEndTime = StringUtils.formatDateToString(new Date(), "yyyy-MM");
        }
    	if (StringUtils.isEmpty(mediaType)) 
    	{
    		mediaType = "00";
        }
    	
    	if (StringUtils.isEmpty(keyword)) 
    	{
    		keyword = "";
        }
    	    	    	
    	if(accrualType == null || accrualType == 0)
    	{
    		accrualType = 1;
    	}
	
    	// 获取当前session对象中所属城市
        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                .getSession()
                .getAttribute(Constants.SESSIONPRKEY);
//	        userInfo.getUserRole().setRoleRange("2");
//            userInfo.setUsercity("CZ");
        
        // 初始化分页对象
		page = new PageBean(currentPage == 0 ? 1 : currentPage,
                Constants.PAGE_SIZE);
    
        // 非空判断 
        if (null != userInfo && accrualCity == null){
        	// 默认东莞
        	accrualCity = "DG";
        	 if (userInfo.getUserRole() != null){
        		 //非空重置
        		 if( null != userInfo.getUsercity()){
        			 if (!"".equals(userInfo.getUsercity()))
        			 {
        				 accrualCity = userInfo.getUsercity();
        			 }
        		 }
    
        	//操作范围
        	String roleRange = userInfo.getUserRole().getRoleRange();
        	if (roleRange!= null){
        		 // 地市级
                if ("2".endsWith(roleRange)||"3".endsWith(roleRange))
                {
					dataListCITY.clear();
					Dictionary dictionary = (Dictionary) DatadicContext
							.getInstance().getDatadicObject(accrualCity,
									"CITY");
					dataListCITY.add(dictionary);

                }
            }
        }
    }
	//查询
	
	Map condition = new HashMap();
    condition.put("i_month", totalAccrualEndTime + "-01");
    condition.put("i_mediatype", mediaType);
    condition.put("i_type", accrualType);
    condition.put("i_searchkey", keyword);
    condition.put("i_resourcetype", "00");
    condition.put("i_projecttype", "00");
    condition.put("i_city", accrualCity);
    condition.put("i_starid", 0);
	condition.put("i_endid", 0);
	
	mediaTypeSelect = DatadicContext.getInstance().getDicDataListByCode("MTLX");
    // 获取总记录数
    condition.put("i_selecttype", 1);
    int totalRecords = accrualService.getTotalAccrualCount(condition); 
    // 设置分页对象总记录数
    page.setTotalRecords(totalRecords);
    //查询结果集
    condition.put("i_selecttype", 2);
	itemAccruals = accrualService.getTotalAccrualList(condition, page);

        // 重置返回
        resultName = SUCCESS;
		
      }catch (Exception e)
        {
	        // 打印异常信息
	        e.printStackTrace();
	        // 打印异常方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.error("Exception AccrualAction totalAccrualList() method",
	                    e);
	        }
	        // 重置返回值
	        resultName = ERROR;
        }

	    // 打印退出方法体日志
	    if (log.isInfoEnabled())
	    {
	        log.info("Exit AccrualAction totalAccrualList() method, return String");
	    }
    
	    return resultName;
	}
	
	//获取业务计提明细（备选）
	@SuppressWarnings("unchecked")
	public String backAccrualList()
	{
	
		// 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter AccrualAction backAccrualList() method, return String");
        }

        try
        {
        	getDictionary();            //获取字典表中归属地
        	if (StringUtils.isEmpty(totalAccrualEndTime)) 
        	{
           	  totalAccrualEndTime = StringUtils.formatDateToString(new Date(), "yyyy-MM");
            }
        	
        	if (StringUtils.isEmpty(mediaType)) 
        	{
        		mediaType = "00";
            }
        
        	if (StringUtils.isEmpty(keywordbx)) 
    		{
        		keywordbx = "";
    		}
        
        	if (null == accrualType && !"".equals(accrualType)) {
        		accrualType = 2;
    		}
        	// 获取当前session对象中所属城市
	        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
	                .getSession()
	                .getAttribute(Constants.SESSIONPRKEY);
//	        userInfo.getUserRole().setRoleRange("2");
//            userInfo.setUsercity("CZ");
	        
	        // 初始化分页对象
			page = new PageBean(currentPage == 0 ? 1 : currentPage,
	                Constants.PAGE_SIZE);
        
			 // 非空判断 
	        if (null != userInfo && accrualCity == null){
	        	// 默认东莞
	   		 	accrualCity = "DG";
	        	 if (userInfo.getUserRole() != null){
	        		 
	        		 //非空重置
	        		 if( null != userInfo.getUsercity()){
	        			 if (!"".equals(userInfo.getUsercity()))
	        			 {
	        				 accrualCity = userInfo.getUsercity();
	        			 }
	        		 }
	    
	        	//操作范围
	        	String roleRange = userInfo.getUserRole().getRoleRange();
	        	if (roleRange!= null){
		        	//省级
	        		 // 地市级
	                if ("2".endsWith(roleRange)||"3".endsWith(roleRange))
	                {
						dataListCITY.clear();
						Dictionary dictionary = (Dictionary) DatadicContext
								.getInstance().getDatadicObject(accrualCity,
										"CITY");
						dataListCITY.add(dictionary);

	                }
	            }
	        }
	    }
		//查询
		Map condition1 = new HashMap();
        condition1.put("i_month", totalAccrualEndTime + "-01");
        condition1.put("i_mediatype", mediaType);
        condition1.put("i_type", accrualType);
        condition1.put("i_searchkey", keywordbx);
        condition1.put("i_resourcetype", "00");
        condition1.put("i_projecttype", "00");
        condition1.put("i_city", accrualCity);
        condition1.put("i_starid", 0);
		condition1.put("i_endid", 0);
		
		mediaTypeSelect = DatadicContext.getInstance().getDicDataListByCode("MTLX");
        
		// 获取总记录数
        condition1.put("i_selecttype", 1);
        int totalRecords = accrualService.getTotalAccrualCount(condition1);
        // 设置分页对象总记录数
        page.setTotalRecords(totalRecords);
        //查询结果集
        condition1.put("i_selecttype", 2);
        itemBackAccruals = accrualService.getTotalAccrualList(condition1, page);

	     // 重置返回
	        resultName = SUCCESS;
		
		}catch (Exception e)
	    {
	        // 打印异常信息
	        e.printStackTrace();
	        // 打印异常方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.error("Exception AccrualAction backAccrualList() method",
	                    e);
	        }
	        // 重置返回值
	        resultName = ERROR;
	    }

    // 打印退出方法体日志
    if (log.isInfoEnabled())
    {
        log.info("Exit AccrualAction backAccrualList() method, return String");
    }
    
    return resultName;
		
	}
		
//获取计提明细（合同）
@SuppressWarnings("unchecked")
public String contractAccrualList()
{

	// 返回值
    String resultName = null;
    // 打印进入方法体日志
    if (log.isInfoEnabled())
    {
        log.info("Enter AccrualAction contractAccrualList() method, return String");
    }

    try
    {
    	getDictionary();            //获取字典表中归属地
    	
    	if (StringUtils.isEmpty(totalAccrualEndTime)) 
    	{
       	  totalAccrualEndTime = StringUtils.formatDateToString(new Date(), "yyyy-MM");
        }
        
        if (StringUtils.isEmpty(keywordht)) 
    	{
    		keywordht = "";
        }
     // 获取当前session对象中所属城市
        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                .getSession()
                .getAttribute(Constants.SESSIONPRKEY);
//        userInfo.getUserRole().setRoleRange("2");
//        userInfo.setUsercity("CZ");
        
        // 初始化分页对象
		page = new PageBean(currentPage == 0 ? 1 : currentPage,
                Constants.PAGE_SIZE);
    
		 // 非空判断 
        if (null != userInfo && accrualCity == null){
        	// 默认东莞
   		 	accrualCity = "DG";
        	 if (userInfo.getUserRole() != null){
        		 
        		 //非空重置
        		 if( null != userInfo.getUsercity()){
        			 if (!"".equals(userInfo.getUsercity()))
        			 {
        				 accrualCity = userInfo.getUsercity();
        			 }
        		 }
    
        	//操作范围
        	String roleRange = userInfo.getUserRole().getRoleRange();
        	if (roleRange!= null){
	        	//省级
        		 // 地市级
                if ("2".endsWith(roleRange)||"3".endsWith(roleRange))
                {
					dataListCITY.clear();
					Dictionary dictionary = (Dictionary) DatadicContext
							.getInstance().getDatadicObject(accrualCity,
									"CITY");
					dataListCITY.add(dictionary);

                }
            }
        }
    }
	//查询
	Map condition2 = new HashMap();
    condition2.put("i_month", totalAccrualEndTime + "-01");
    condition2.put("i_searchkeytype",keywordList);
    condition2.put("i_searchkey", keywordht);
    condition2.put("i_city", accrualCity);
    condition2.put("i_starid", 0);
	condition2.put("i_endid", 0);
	
	// 获取总记录数
    condition2.put("i_selecttype", 1);
    int totalRecords = accrualService.getContractAccrualCount(condition2); 
    // 设置分页对象总记录数
    page.setTotalRecords(totalRecords);
    //查询结果集
    condition2.put("i_selecttype", 2);
    itemContractAccruals = accrualService.getContractAccrualList(condition2, page);

        // 重置返回
        resultName = SUCCESS;
		
	}catch (Exception e)
    {
        // 打印异常信息
        e.printStackTrace();
        // 打印异常方法体日志
        if (log.isInfoEnabled())
        {
            log.error("Exception AccrualAction contractAccrualList() method",e);
        }
        // 重置返回值
        resultName = ERROR;
    }

    // 打印退出方法体日志
    if (log.isInfoEnabled())
    {
        log.info("Exit AccrualAction contractAccrualList() method, return String");
    }
    
    return resultName;
	}
	
	//初始化归属地
	public void getDictionary() {
//		dataListCITY = DatadicContext.getInstance().getSubDatadicByCode("-1","CITY");// 归属地市
		dataListCITY = DatadicContext.getInstance().getDicDataListByCode("CITY");// 归属地市
//		if(dataListCITY==null)
//		{
//			dataListCITY = new ArrayList<Dictionary>();
//		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
    
	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public AccrualService getAccrualService() {
		return accrualService;
	}

	public void setAccrualService(AccrualService accrualService) {
		this.accrualService = accrualService;
	}

	public List<ItemAccrual> getItemAccruals() {
		return itemAccruals;
	}

	public void setItemAccruals(List<ItemAccrual> itemAccruals) {
		this.itemAccruals = itemAccruals;
	}

	public List<ItemAccrual> getItemBackAccruals() {
		return itemBackAccruals;
	}

	public void setItemBackAccruals(List<ItemAccrual> itemBackAccruals) {
		this.itemBackAccruals = itemBackAccruals;
	}

	public List<ContractAccrual> getItemContractAccruals() {
		return itemContractAccruals;
	}

	public void setItemContractAccruals(List<ContractAccrual> itemContractAccruals) {
		this.itemContractAccruals = itemContractAccruals;
	}

	 public String getAccrualCity() {
		return accrualCity;
	}

	public void setAccrualCity(String accrualCity) {
		this.accrualCity = accrualCity;
	}

	public List<Dictionary> getDataListCITY() {
		return dataListCITY;
	}

	public void setDataListCITY(List<Dictionary> dataListCITY) {
		this.dataListCITY = dataListCITY;
	}
		
	public String getTotalAccrualEndTime() {
		return totalAccrualEndTime;
	}

	public void setTotalAccrualEndTime(String totalAccrualEndTime) {
		this.totalAccrualEndTime = totalAccrualEndTime;
	}

	public String getMediaType() {
		return mediaType;
	}

	public void setMediaType(String mediaType) {
		this.mediaType = mediaType;
	}
	
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<Dictionary> getMediaTypeSelect() {
		return mediaTypeSelect;
	}

	public void setMediaTypeSelect(List<Dictionary> mediaTypeSelect) {
		this.mediaTypeSelect = mediaTypeSelect;
	}
	 
    public int getKeywordList() {
		return keywordList;
	}

	public void setKeywordList(int keywordList) {
		this.keywordList = keywordList;
	}
	
	 public Integer getAccrualType() {
			return accrualType;
	}

	public void setAccrualType(Integer accrualType) {
		this.accrualType = accrualType;
	}

	public String getKeywordht() {
		return keywordht;
	}

	public void setKeywordht(String keywordht) {
		this.keywordht = keywordht;
	}

	public String getKeywordbx() {
		return keywordbx;
	}

	public void setKeywordbx(String keywordbx) {
		this.keywordbx = keywordbx;
	}
}
